import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);

import Login from "@/components/Login.vue";
import Home from "@/components/Home.vue";
import Users from '@/components/User/Users.vue'
import Roles from "@/components/Right/Roles.vue"
import Rights from "@/components/Right/Rights.vue"
import Categories from '@/components/Shop/Categories.vue'
import Goods from '@/components/Shop/Goods.vue'
import Params from '@/components/Shop/Params.vue'
import Orders from '@/components/Order/orders.vue'
import Reports from "@/components/Report/reports.vue"
import AddShop from "@/components/Shop/AddShop.vue"


const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  routes: [
    {
      path: "/login",
      name: "login",
      component: Login,
    },
    {
      path: "/home",
      name: "home",
      component: Home,
      children: [
        {
          path: "/home/",
          redirect:'/home/users'
        },
        {
          path: "/home/users",
          name: "users",
          component: Users,
        },
        {
          path: "/home/roles",
          name: "roles",
          component: Roles,
        },
        {
          path: "/home/rights",
          name: "rights",
          component: Rights,
        },
        {
          path: "/home/categories",
          name: "categories",
          component: Categories,
        },
        {
          path: "/home/goods",
          name: "goods",
          component: Goods,
        },
        {
          path:"/home/goods/add",
          component:AddShop
        },
        {
          path: "/home/params",
          name: "params",
          component: Params,
        },
        {
          path: "/home/orders",
          name: "orders",
          component: Orders,
        },
        {
          path: "/home/reports",
          name: "reports",
          component: Reports,
        }
      ],
    },
  ],
});

router.beforeEach((to, from, next) => {
  if (to.path === "/login") return next();
  const token = window.sessionStorage.getItem("token");
  if (!token) {
    return next("/login");
  }
  next();
});

export default router;
